package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 分页查询学生列表
     * @param studentQueryParam
     * @return
     */
    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam) {
        //设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        Page<Student> page = studentMapper.list(studentQueryParam);
        return new PageResult<>(page.getTotal(), page.getResult());

    }
    /**
     * 删除学生
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }
    /**
     * 添加学生
     * @param student
     */
    @Override
    public void add(Student student) {
        studentMapper.add(student);
    }
    /**
     * 根据id查询学生
     * @param id
     * @return
     */
    @Override
    public Student selectById(Integer id) {
        Student student = studentMapper.selectById(id);
        return student;
    }

    /**
     * 修改学生信息
     * @param student
     */
    @Override
    public void update(Student student) {
        studentMapper.update(student);
    }
    /**
     * 扣分
     * @param id
     * @param score
     */
    @Override
    public void violate(Integer id, Integer score) {
        if (score <= 0){
            throw new RuntimeException("扣分不能小于0");
        }
        studentMapper.violate(id,score);
    }
}
